package joinmessage

import (
	"admin.supervise.limaochuxing.top/admin/schemas/req"
	"admin.supervise.limaochuxing.top/admin/service/joinmessage"
	"admin.supervise.limaochuxing.top/config"
	"admin.supervise.limaochuxing.top/core"
	"admin.supervise.limaochuxing.top/core/request"
	"admin.supervise.limaochuxing.top/core/response"
	"admin.supervise.limaochuxing.top/middleware"
	"admin.supervise.limaochuxing.top/util"

	"github.com/gin-gonic/gin"
)

var JoinMessageGroup = core.Group("/join-message", newJoinMessageHandler, regJoinMessage, middleware.TokenAuth())

func newJoinMessageHandler(srv joinmessage.IJoinMessageService) *joinMessageHandler {
	return &joinMessageHandler{srv: srv}
}

func regJoinMessage(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *joinMessageHandler) {
		rg.GET("/list", handle.list)
		rg.POST("/follow", handle.follow)
		rg.POST("/remark", handle.remark)
	})
}

type joinMessageHandler struct {
	srv joinmessage.IJoinMessageService
}

// list 获取登记信息列表
func (h *joinMessageHandler) list(c *gin.Context) {
	var page request.PageReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
		return
	}

	var listReq req.JoinMessageListReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
		return
	}

	data, err := h.srv.List(page, listReq)
	response.CheckAndRespWithData(c, data, err)
}

// follow 跟进登记信息
func (h *joinMessageHandler) follow(c *gin.Context) {
	var followReq req.JoinMessageFollowReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &followReq)) {
		return
	}

	followReq.FollowId = config.AdminConfig.GetAdminId(c)
	err := h.srv.Follow(followReq)
	response.CheckAndResp(c, err)
}

// remark 更新备注信息
func (h *joinMessageHandler) remark(c *gin.Context) {
	var remarkReq req.JoinMessageRemarkReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &remarkReq)) {
		return
	}

	err := h.srv.UpdateRemark(remarkReq)
	response.CheckAndResp(c, err)
}
